﻿using System;
using System.Collections.Generic;
using ZyGames.DZPocker.Model;
using ZyGames.Framework.Cache.Generic;
using ZyGames.Framework.Game.Cache;
using ZyGames.Framework.Game.Context;
using ZyGames.Framework.Game.Contract;
using ZyGames.Framework.Game.Contract.Action;
using ZyGames.Framework.Game.Lang;
using ZyGames.Framework.Game.Service;
using System.Linq;
using ZyGames.DZPocker.Bll.Logic;
using ZyGames.DZPocker.Script;
using System.Text.RegularExpressions;
using ZyGames.Framework.Net;
using ZyGames.DZPocker.Tools;

namespace ZyGames.DZPocker.Script.CsScript.Action
{
	/// <summary>
	/// 响应玩家点击好友头像的操作
	/// </summary>
	public class Action30091 : BaseAction
	{
		private int tableId;
		private TableData _currentTable;
		private int btnType;
		/// <summary>
		/// 主动点击的玩家Id
		/// </summary>
		private int clickedUserID;
		/// <summary>
		/// 被动的玩家Id
		/// </summary>
		private int beClickedUserID;

		private bool result = false;

		public Action30091(HttpGet actionGetter)
			: base((short)ActionType.DZResponseClickPlayerHeadIcon, actionGetter)
		{
		}
		public override bool GetUrlElement()
		{
			if (httpGet.GetInt("TableId", ref tableId) &&
				httpGet.GetInt("BtnType", ref btnType) &&
				httpGet.GetInt("ClickedUserID", ref clickedUserID) &&
				httpGet.GetInt("BeClickedUserID", ref beClickedUserID))
			{
				return true;
			}
			return false;
		}

		public override bool TakeAction()
		{
			if(clickedUserID == beClickedUserID)
			{
				Tool.WriteWarmingLine(string.Format("桌子:{0} 两个人好友操作ID一样，不合法", tableId));
				return false;
			}
			_currentTable = GameRoom.Current.GetTableData(tableId);
			if (_currentTable == null)
			{
				Tool.WriteErrorLine("消息30091获得房间不存在,房间ID:" + tableId);
				return false;
			}

			var firstUser = GameTable.Current.GetUser(clickedUserID);
			if (firstUser == null)
			{
				Tool.WriteWarmingLine("消息30091玩家1不存在");
				return false;
			}
			var secondUser = GameTable.Current.GetUser(beClickedUserID);
			if (firstUser == null)
			{
				Tool.WriteWarmingLine("消息30091玩家2不存在");
				return false;
			}

			var friendBtnType = (FriendShipButtonType)btnType;
			if(!Enum.IsDefined(typeof(FriendShipButtonType), friendBtnType))
			{
				Tool.WriteErrorLine("消息30091发送的按钮类型不正确,类型是:" + btnType);
				return false;
			}

			switch(friendBtnType)
			{
				case FriendShipButtonType.Add:
					{
						if(!GameFriendShip.Current.CreateFrindShip(clickedUserID, beClickedUserID))
						{
							Tool.WriteErrorLine("消息30091添加好友关系失败");
							return false;
						}
					}
					break;
				case  FriendShipButtonType.Delete:
					{
						if (!GameFriendShip.Current.DeleteFriendShip(clickedUserID, beClickedUserID))
						{
							Tool.WriteErrorLine("消息30091删除好友关系失败");
							return false;
						}
					}
					break;
				//房主点击移除座位,强制某人站起
				case FriendShipButtonType.RemoveFromPos:
					{
						try
						{
							//var pos = _currentTable.Positions.ToList<PositionData>().FirstOrDefault(i => i.UserId == beClickedUserID);
							PositionData tempPos = null;
							foreach(var pos in _currentTable.Positions)
							{
								if(pos.UserId == beClickedUserID)
								{
									tempPos = pos;
									break;
								}
							}
							if (tempPos == null)
							{
								Tool.WriteErrorLine(string.Format("桌子：{0} 没有找到userID为：{1}的玩家所在的桌子", tableId, beClickedUserID));
								return false;
							}
							var beClickedPosId = tempPos.Id;
							if (beClickedPosId == 0)
							{
								Tool.WriteErrorLine(string.Format("桌子：{0} 找到userID为：{1}的玩家不再桌子座位{2}上", tableId, beClickedUserID, beClickedPosId));
								return false;
							}
							//下发20009站起消息
							var parameters = new Parameters();
							parameters["TableID"] = tableId;
							parameters["UserID"] = beClickedUserID;
							parameters["PosID"] = beClickedPosId;
							GameTable.Current.SyncNotifyAction((int)ActionType.DZGameUserStandUp, secondUser, parameters, t =>
							{
								Console.WriteLine("下发20009某人离开座位的消息");
							});
						}
						catch(Exception ex)
						{
							Tool.WriteErrorLine(ex.ToString());
						}
						
					}
					break;
				default:
					{
						Tool.WriteErrorLine("30091消息 按钮类型不存在");
						return false;
					}
			}
			this.result = true;
			return true;
		}

		//关闭弹框
		public override void BuildPacket()
		{
			this.PushIntoStack(this.tableId);
			this.PushIntoStack(this.result);
		}
	}
}
